package com.zxj.schoolxianyu.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zxj.schoolxianyu.dao.buyrecordDao;
import com.zxj.schoolxianyu.dao.goodsDao;
import com.zxj.schoolxianyu.entity.buyrecord;
import com.zxj.schoolxianyu.entity.goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class buyService {

    @Autowired(required = false)
    private buyrecordDao buyrecordDao;
    @Autowired(required = false)
    private goodsDao goodsDao;

    public String buy(int goodsid, String username, int price) {
        buyrecord record = new buyrecord();
        record.goodsid = goodsid;
        record.username = username;
        record.price = price;
        buyrecordDao.insert(record);
        EntityWrapper<goods> wrapper = new EntityWrapper<>();
        wrapper.eq("goodsid", goodsid);
        goods g = goodsDao.selectList(wrapper).get(0);
        g.state = 0;
        goodsDao.update(g, wrapper);
        return "success";
    }
}
